package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmDataAuthTmpl;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmAuthRecoMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmDataAuthMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmDataAuthTmplMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * @version 1.0.0
 * @项目名称: yusp-admin
 * @类名称: AdminSmDataAuthTmplService
 * @类描述: 数据权限模板
 * @功能描述:
 * @创建人: liaoxd@yusys.com.cn
 * @创建时间: 2017-12-27 18:09
 * @修改备注:
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmDataAuthTmplService extends CommonService {
    @Autowired
    private AdminSmDataAuthTmplMapper mapper;
    @Autowired
    private AdminSmDataAuthMapper adminSmDataAuthMapper;
    @Autowired
    private AdminSmAuthRecoMapper adminSmAuthRecoMapper;

    @SuppressWarnings("rawtypes")
    @Override
    protected CommonMapper getMapper() {
        return this.mapper;
    }

    @Override
    public int insert(Object record) {
        AdminSmDataAuthTmpl adminSmDataAuthTmpl = (AdminSmDataAuthTmpl) record;
        adminSmDataAuthTmpl.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.insert(getMapper(), record);
    }

    @Override
    public int updateSelective(Object record) {
        AdminSmDataAuthTmpl adminSmDataAuthTmpl = (AdminSmDataAuthTmpl) record;
        adminSmDataAuthTmpl.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.updateSelective(getMapper(), record);
    }

    /**
     * @函数名称:deletes
     * @函数描述:级联删除数据权限和授权关系
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public int deletes(String id) {
        String tmplId[] = new String[1];
        tmplId[0] = id;
        List<String> list = this.adminSmDataAuthMapper.getDataAuthId(tmplId);
        if (list.size() > 0) {
            String[] authIds = new String[list.size()];
            list.toArray(authIds);
            //先删除关联数据
            this.adminSmAuthRecoMapper.deleteDataAuth(authIds);
        }
        //再删除权限数据
        this.adminSmDataAuthMapper.deleteDataInfo(tmplId);
        int i = this.mapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * @函数名称:selectByModel
     * @函数描述:重新，支持页面模糊查询
     * @参数与返回说明:
     * @算法描述:
     */
    @SuppressWarnings("unchecked")
    @Override
    public <T> List<T> selectByModel(QueryModel model) {
        //模糊条件
        if (model.getCondition().containsKey("authTmplName")) {
            model.getCondition().put("authTmplName", "%" + model.getCondition().get("authTmplName") + "%");
        }
        if (model.getCondition().containsKey("sqlName")) {
            model.getCondition().put("sqlName", "%" + model.getCondition().get("sqlName") + "%");
        }

        // 设置分页查询参数(设置到线程变量中了)
        PageHelper.startPage(model.getPage(), model.getSize());
        List<Map<String, String>> list = this.mapper.getTmplListBycodeOrName(model);
        // 清除线程绑定的分页参数，防止影响同线程的其他查询
        PageHelper.clearPage();

        return (List<T>) list;
    }
}
